package it.unibo.imola.parser.visitor.entity;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;

public abstract class DeclarationStatement extends Statement {

	protected String varName;

	public DeclarationStatement(String varName) {
		if(varName == null)
			throw new IllegalArgumentException("varName == null");
		this.varName = varName;
	}

	/**
	 * @return the varName
	 */
	public String getVarName() {
		return varName;
	}

	/* (non-Javadoc)
	 * @see it.unibo.imola.parser.evaluation.entity.Instruction#check(it.unibo.imola.parser.evaluation.entity.Environment)
	 */
	@Override
	public boolean check(Environment env) throws Exception {
		super.check(env);
		if(!env.getVariables().containsKey(varName)){
			setVariable(env);
			return true;
		}
		return false;
	}
	
	protected abstract void setVariable(Environment env);

	/* (non-Javadoc)
	 * @see it.unibo.imola.parser.evaluation.entity.Instruction#exec(it.unibo.imola.parser.evaluation.entity.Environment)
	 */
	@Override
	public void exec(Environment env) throws Exception {
		super.exec(env);
		setVariable(env);
	}

	@Override
	public String toString() {
		return ReflectionToStringBuilder.toString(this);
	}
	
	
}
